package flexo.input.entity;

import flexo.input.Event;
import flexo.math.Vector;
import flexo.scene.Node;

public class FPSMovement extends Entity
{
	protected Node target;
	protected static final Vector up = new Vector(0,1,0);
	
	public static final String MOVE_FORWARD = "FPS_MOVE_FORWARD";
	public static final String MOVE_BACKWARD = "FPS_MOVE_BACKWARD";
	public static final String STRAFE_LEFT = "FPS_STRAFE_LEFT";
	public static final String STRAFE_RIGHT = "FPS_STRAFE_RIGHT";
	
	public FPSMovement(Node target)
	{
		this.target = target;
		setSensitivity(3.0f);
	}

	@Override
	protected void update(Event e, float value)
	{
		Vector forward = Vector.getTempVector();
		Vector.sub(target.getAbsolutePosition(), exclusiveNode.getAbsolutePosition(), forward);
		forward.normalize();
		Vector vector = null;
		
		if (e.check(MOVE_FORWARD))
		{
			forward.scale(value);
			vector = forward;			
		}
		else if (e.check(MOVE_BACKWARD))
		{
			forward.scale(-value);
			vector = forward;
		}
		else if (e.check(STRAFE_LEFT))
		{
			vector = Vector.getTempVector();
			Vector.cross(forward, up, vector);
			vector.normalize();
			vector.scale(-value);
		}
		else if (e.check(STRAFE_RIGHT))
		{
			vector = Vector.getTempVector();
			Vector.cross(forward, up, vector);
			vector.normalize();
			vector.scale(value);
		}
		if (exclusiveNode != null && vector != null)
		{
			exclusiveNode.move(vector);
		}
	}
}
